<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小夏 < 449134904@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\admin\model\ApprovalModel;
use app\admin\model\OrderModel;
use app\admin\model\OrderPayLogModel;
use app\admin\model\OrdersModel;
use app\admin\model\PayDepositModel;
use app\admin\model\PayRefundModel;
use app\admin\model\ProtectPaymentModel;
use app\admin\model\ServiceBuriedModel;
use app\admin\model\ServiceDepositModel;
use cmf\controller\AdminBaseController;
use think\Db;

class RemindController extends AdminBaseController
{

    public function initialize()
    {
        $adminSettings = cmf_get_option('admin_settings');
        if (empty($adminSettings['admin_password']) || $this->request->path() == $adminSettings['admin_password']) {
            $adminId = cmf_get_current_admin_id();
            if (empty($adminId)) {
                session("__LOGIN_BY_CMF_ADMIN_PW__", 1);//设置后台登录加密码
            }
        }
        parent::initialize();
    }
	public function index(){

        $dj_arr = PayDepositModel::where(['status'=>0])->where('add_time','<=',strtotime("-24 hour"))->select();
        foreach ($dj_arr as $key=>&$value){
            PayDepositModel::update(['status'=>3],['id'=>$value['pay_id']]);
            OrderPayLogModel::update(['status'=>-1],['id'=>$value['pay_id']]);
            OrderModel::update(['status'=>-1,'is_delete'=>1-1],['order_id'=>$value['order_id']]);
        }
        $cate = $this->request->param('cate');
        $type = $this->request->param('type');
        $buyer_name = $this->request->param('buyer_name');
        $phone = $this->request->param('phone');
        $start_time = $this->request->param('start_time');
        $end_time = $this->request->param('end_time');

        $cate = $cate?$cate:"shouqian";
        $type = $type?$type:"dingjin";

        $where[] = array('1',"=",1);
        if(!empty($buyer_name)){
            $where[] = array('c.buyer_name','like',"%".$buyer_name."%");
        }
        if(!empty($phone)){
            $where[] = array('c.buyer_phone','like',"%".$phone."%");
        }
        if (empty($start_time)){
            $start_time = -99999999999;
        }else{
            $start_time = strtotime($start_time);
        }
        if (empty($end_time)) {
            $end_time = 99999999999;
        }else{
            $end_time = strtotime($end_time . '+1 day');
        }

        // 落葬提醒
        $buried_field = 's.buried_time,s.dealer_user,s.dealer_phone,s.tomb_num,tomb_address,s.handle_name,s.service_status';

        $buried_results_array = Db::name("service_buried")->alias("s")
            ->join("dj_customer_info c","s.customer_id = c.id")
            ->field($buried_field)
            ->where($where)
            ->where("s.pay_status = 1 ")
            ->where("s.service_status < 3 ")
            ->where('s.buried_time','>=',$start_time)
            ->where('s.buried_time','<=',$end_time)
            ->order('s.add_time desc')
            ->select();
        $this->assign("buried_count",count($buried_results_array));
        if (count($buried_results_array)!=0){
            $buried_results[] = $buried_results_array[0];
        }else{
            $buried_results=[];
        }
        $this->assign("buried_results",$buried_results);

        // 维保提醒
        $weibao_results = Db::name("serviceWeibao")->alias("w")
            ->field("w.*,c.buyer_name,c.buyer_phone,c.customer_address")
//            ->leftJoin("dj_maintenance_project p","w.id = p.wb_id")
            ->leftJoin("dj_customer_info c","w.customer_id = c.id")
            ->where($where)
            ->where("w.pay_status = 1 ")
            ->where("w.service_status <> 3 ")
            ->where('w.deliver_time','>=',$start_time)
            ->where('w.deliver_time','<=',$end_time)
            ->order('w.add_time desc')
            ->select();
//        dump(Db::name("serviceWeibao")->getLastSql());exit;
        $this->assign("weibao_count",count($weibao_results));
        if (count($weibao_results)!=0){
            $weibao_results_[] = $weibao_results[0];
        }else{
            $weibao_results_=[];
        }
        $this->assign("weibao_results",$weibao_results_);

        //维护费提醒
        $weihu_results = Db::name('protect_payment')->alias('p')
            ->field('p.end_time,p.duration,c.buyer_name,buyer_phone,t.tomb_num,t.tomb_region,t.tomb_address')
            ->join("dj_customer_info c","  c.id = p.customer_id")
            ->join("dj_tombarea_information_product t"," t.tomb_id=p.mw_id ")
            ->where('p.pay_status',0)
            ->where('p.end_time','>=',strtotime("-6 month",time()))
            ->where('c.buyer_name','like',"%".$buyer_name."%")
            ->where('c.buyer_phone','like',"%".$phone."%")
            ->where('p.create_time','>=',$start_time)
            ->where('p.create_time','<=',$end_time)
            ->order('p.create_time desc')
            ->select();
        $this->assign("weihu_count",count($weihu_results));
        if (count($weihu_results)!=0){
            $weihu_results_[] = $weihu_results[0];
        }else{
            $weihu_results_=[];
        }

        $this->assign("weihu_result",$weihu_results_);

        // 寄存提醒
        $deposit_results = Db::name("serviceDeposit")->alias("d")
            ->join("dj_customer_info c","d.customer_id = c.id")
            ->where($where)
            ->where('d.submit_status','>',1)
            ->where('d.type','=',0)
            ->where("d.pay_status = 1 ")
            ->where("d.service_status < 3 ")
            ->where('d.deposit_time','>=',$start_time)
            ->where('d.deposit_time','<=',$end_time)
            ->order('d.add_time desc')
            ->select();
        if (count($deposit_results)!=0){
            $deposit_results_[] = $deposit_results[0];
        }else{
            $deposit_results_=[];
        }
        $this->assign("jicun_count",count($deposit_results)); 
        $this->assign("deposit_results",$deposit_results_);

        // 寄出提醒
        $deposit_results_out = Db::name("serviceDeposit")->alias("d")
            ->join("dj_customer_info c","d.customer_id = c.id")
            ->where($where)
            ->where('d.type','=',1)
            ->where("d.pay_status = 1 ")
            ->where("d.service_status < 3 ")
            ->where('d.deposit_time','>=',$start_time)
            ->where('d.deposit_time','<=',$end_time)
            ->order('d.add_time desc')
           ->select();
        $this->assign("deposit_out_count",count($deposit_results_out));
        if (count($deposit_results_out)!=0){
            $deposit_results_out_[] = $deposit_results_out[0];
        }else{
            $deposit_results_out_=[];
        }

        $this->assign("deposit_results_out",$deposit_results_out_);

        // 迁墓提醒
        $move_results = Db::name("serviceMove")->alias("m")
            ->join("dj_customer_info c","m.customer_id = c.id")
            ->where($where)
            ->where("m.pay_status = 1 ")
            ->where("m.service_status < 3 ")
            ->where('m.submit_status','>',1)
            ->where('m.move_time','>=',$start_time)
            ->where('m.move_time','<=',$end_time)
            ->order('m.add_time desc')
            ->select();
        $this->assign("move_count",count($move_results));
        if (count($move_results)!=0){
            $move_results_[] = $move_results[0];
        }else{
            $move_results_=[];
        }

        $this->assign("move_results",$move_results_);

        $user = Db::name("user")->where('id',cmf_get_current_admin_id())->find();
        // 判断如果墓位是艺术墓、且没有销售艺术墓权限时，不显示艺术墓订单
        if($user['if_sale'] == 1){
            // 订单提醒
            $order_results = Db::name("orders")->alias("o")
                ->field("o.*,u.user_nickname")
                ->join("dj_user u","u.id = o.edit_user")
                //->join("dj_tombarea_row_col t","t.colID = o.tomb_id")
                ->where("o.customer_name","like","%".$buyer_name."%")
                ->where("o.customer_phone","like","%".$phone."%")
                ->where('o.order_status','=',1)
                ->where('o.add_time','>=',$start_time)
                ->where('o.add_time','<=',$end_time)
                ->order('o.add_time desc')
                ->select();

            $this->assign("order_count",count($order_results));
            if (count($order_results)!=0){
                $order_results_[] = $order_results[0];
            }else{
                $order_results_=[];
            }
            $this->assign("order_results",$order_results_);

            // 定金提醒
            $sub_results = Db::name("pay_deposit")->alias("p")
                ->field("p.*,o.selling_price")
                ->join("dj_order o","p.order_id = o.order_id")
                ->join("dj_tombarea_row_col t","t.colID = o.tomb_id")
                ->where("p.buyer_name","like","%".$buyer_name."%")
                ->where("p.buyer_phone","like","%".$phone."%")
                ->where('p.dtype','=',1)
                ->where('p.status','=',1)
                ->where('o.add_time','>=',$start_time)
                ->where('o.add_time','<=',$end_time)
                ->where('p.refund_state' ,0)
                ->order('o.add_time desc')
                ->select();
            $this->assign("orders_count",count($sub_results));
        }else{
            // 订单提醒
            $order_results = Db::name("orders")->alias("o")->field("o.*,u.user_nickname")
                ->join("dj_tombarea_row_col t","t.colID = o.tomb_id")
                ->join("dj_user u","u.id = o.edit_user")
                ->where("o.customer_name","like","%".$buyer_name."%")
                ->where("o.customer_phone","like","%".$phone."%")
                ->where('o.order_status','=',1)
                ->where('t.cate_id','<>',2)
                ->where('o.add_time','>=',$start_time)
                ->where('o.add_time','<=',$end_time)
                ->order('o.add_time desc')
                ->select();
            $this->assign("order_count",count($order_results));
            if (count($order_results)!=0){
                $order_results_[] = $order_results[0];
            }else{
                $order_results_=[];
            }
            $this->assign("order_results",$order_results_);

            // 定金提醒
            $sub_results = Db::name("pay_deposit")->alias("p")
                ->field("p.*,o.selling_price")
                ->join("dj_order o","p.order_id = o.order_id")
                ->join("dj_tombarea_row_col t","t.colID = o.tomb_id")
                ->where("p.buyer_name","like","%".$buyer_name."%")
                ->where("p.buyer_phone","like","%".$phone."%")
                ->where('p.dtype','=',1)
                ->where('p.refund_state' ,0)
                ->where('t.cate_id','<>',2)
                ->where('o.add_time','>=',$start_time)
                ->where('o.add_time','<=',$end_time)
                ->order('o.add_time desc')
                ->select();
            $this->assign("orders_count",count($sub_results));
        }

        $this->assign("cate",$cate);
        $this->assign("type",$type);

        if (count($sub_results)!=0){
            $sub_results_[] = $sub_results[0];
        }else{
            $sub_results_=[];
        }

        $this->assign("sub_results",count($sub_results));
        $this->assign("sub_results",$sub_results_);

		return $this->fetch();
	}

	public function deposit($ajax=0){

        $page = $this->request->get("page", 0);
        $limit = $this->request->get("limit", 10);
        $real_name = $this->request->get("real_name", '');
        $real_phone = $this->request->get("real_phone", '');
        $start_time = $this->request->get("start_time", '');
        $end_time = $this->request->get("end_time", '');
        $type = $this->request->get("type", '');


        $cate = $this->request->param('cate');
        $type = $this->request->param('type');
        $buyer_name = $this->request->param('buyer_name');
        $phone = $this->request->param('phone');
        $start_time = $this->request->param('start_time');
        $end_time = $this->request->param('end_time');

        $cate = $cate?$cate:"shouqian";
        $type = $type?$type:"dingjin";

        $where[] = array('service_status',"=",1);
        if(!empty($buyer_name)){
            $where[] = array('c.buyer_name','like',"%".$buyer_name."%");
        }
        if(!empty($phone)){
            $where[] = array('c.buyer_phone','like',"%".$phone."%");
        }
        if (empty($start_time)){
            $start_time = 0;
        }else{
            $start_time = strtotime($start_time);
        }
        if (empty($end_time)) {
            $end_time = 99999999999;
        }else{
            $end_time = strtotime($end_time . '+1 day');
        }

        $this->assign("cate",$cate);
        $this->assign("type",$type);

        if ($ajax==1){
            $tpe = $this->request->param('t');
//            $payDepositModel = new PayDepositModel();
            $payDepositModel = Db::name('PayDeposit')->where(['dtype'=>1]);
            if ($tpe==1){
                $payDepositModel = $payDepositModel->where(['status'=>1])->where('refund_state',0);
            }elseif ($tpe==2){
                $payDepositModel = $payDepositModel->where(['status'=>2,'refund_state'=>0]);
            }elseif ($tpe==3){
                $time = time();
                $payDepositModel = $payDepositModel->where("( status = 1 or status=3 ) and end_tine < {$time}")->where('refund_state',0);

            }elseif ($tpe==4){
                $payDepositModel = $payDepositModel->where('refund_state','>',0);
            }

            $list = $payDepositModel->limit($page,$limit)->select();
            $total = $payDepositModel->count();
            $arr_list =[];
            foreach ($list as $k => &$val) {
                $val['handle_name'] = cmf_get_admin_name($val['handle']);
                $val['cart_type'] = cart_type('cart',$val['cart_type']);
                $val['end_time'] = unixtime_to_date($val['end_time']);
                $val['add_time'] = unixtime_to_date($val['add_time']);
                $arr_list[] = $val;
            }
            $result = array("total" => $total, "rows" => $arr_list,'arr'=>[$tpe]);
            return json($result);
        }
        return $this->fetch();
    }

	/*查询*/
	public function check($ajax=0){
        $type = $this->request->param('type','');
        $t = $this->request->param('t',1);
        $page = $this->request->get("page", 0);
        $limit = $this->request->get("limit", 10);
        $buyer_phone = $this->request->get("real_phone", '');
        $buyer_name = $this->request->get("real_name", '');

        $start_time = $this->request->param('start_time','');
        $end_time = $this->request->param('end_time','');

        if (!empty($start_time)){
            $start_time = strtotime($start_time);
        }
        if (!empty($end_time)) {
            $end_time = strtotime($end_time . '+1 day');
        }

        $where = [];
        if(!empty($buyer_name)){
            $where[] = array('c.buyer_name|pd.buyer_name','like',"%".$buyer_name."%");
        }
        if(!empty($buyer_phone)){
            $where[] = array('c.buyer_phone','like',"%".$buyer_phone."%");
        }
        if ($ajax==1){
            $arr_list=[];
            $total=0;
            if ($type == 'dj'){
                $payDepositModel = Db::name('PayDeposit')->alias('pd')->field('pd.*')
                    ->join("dj_customer_info c","pd.customer_id = c.id")->where(['pd.dtype'=>1]);
                if ($start_time){
                    $payDepositModel= $payDepositModel->where('pd.add_time','>=',$start_time);
                }
                if ($end_time){
                    $payDepositModel= $payDepositModel->where('pd.add_time','<=',$end_time);
                }
                if ($t==1){
                    $payDepositModel = $payDepositModel->where(['pd.refund_state'=>0])->where('pd.status','lt',2);
                }elseif ($t==2){
                    $payDepositModel = $payDepositModel->where(['pd.status'=>2,'pd.refund_state'=>0]);
                }elseif ($t==3){
                    $time = time();
                    $payDepositModel = $payDepositModel->where("( pd.status = 1 or pd.status = 3 ) and pd.end_time < {$time}")->where('pd.refund_state',0);
                }elseif ($t==4){
                    $payDepositModel= $payDepositModel->where('pd.refund_state','>',0);
                }

                // 判断如果墓位是艺术墓、且没有销售艺术墓权限时，不显示艺术墓订单
                $user = Db::name("user")->where('id',cmf_get_current_admin_id())->find();
                if($user['if_sale'] == 0){
                    $payDepositModel =  $payDepositModel->join("dj_order o","pd.order_id = o.order_id")->join("dj_tombarea_row_col t","t.colID = o.tomb_id")->where('t.cate_id','<>',2);
                }
                $list = $payDepositModel->where($where)->limit($page,$limit)->select();
                $total = $payDepositModel->where($where)->count();
                $arr_list =[];
                foreach ($list as $k => &$val) {
                    $val['handle_name'] = cmf_get_admin_name($val['handle']);
                    $val['cart_type'] = cart_type('cart',$val['cart_type']);
                    $val['end_time'] = unixtime_to_date($val['end_time'],'Y-m-d');
                    $val['add_time'] = unixtime_to_date($val['add_time']);
                    $val['djtx_tk_url'] =url('Remind/return1')."?pdid={$val['id']}";
                    $val['djtx_wk_url'] =url('orders/signContract')."?order_id={$val['order_id']}&customer_id={$val['customer_id']}";
                    $val['bddj_url'] = url('Print/print_cert')."?type=1&pay_id={$val['pay_id']}"; //单据
                    $arr_list[] = $val;
                }
            }elseif ($type=='lz'){
                if ($t==2){
                    $lz_where[] = ['service_status','eq',3];
                }else{
                    $lz_where[]  = array('service_status',"lt",3);
                }
                $modelBuried = new ServiceBuriedModel();
                $modelBuried = $modelBuried->alias("pd")->join("dj_customer_info c","pd.customer_id = c.id")
                    ->where(["pd.pay_status" => 1] )->where($where)->where($lz_where);

                if ($start_time){
                    $modelBuried = $modelBuried->where('pd.add_time','>=',$start_time);
                }
                if ($end_time){
                    $modelBuried = $modelBuried->where('pd.add_time','<=',$end_time);
                }

                $results = $modelBuried->order('pd.add_time desc')->limit($page,$limit)->select();

                foreach ($results as $k => &$val) {
                    $val['buried_time'] = unixtime_to_date($val['buried_time']);
                    $arr_list[] = $val;
                }
                $total = $modelBuried->count();
            }elseif ($type=='jc'){
                if ($t==2){
                    $lz_where[] = ['service_status','eq',3];
                    $lz_where[] = ['submit_status','eq',2];
                }else{
                    $lz_where[] = array('submit_status',"gt",1);
                    $lz_where[]  = array('service_status',"lt",3);
                }
                $modeljc = new ServiceDepositModel();
                $modeljc = $modeljc->alias("pd")
                    ->join("dj_customer_info c","pd.customer_id = c.id")
                    ->where(['pd.pay_status'=>1,'pd.type'=>0])
                    ->where($where)->where($lz_where);

                if ($start_time){
                    $modeljc = $modeljc->where('pd.add_time','>=',$start_time);
                }
                if ($end_time){
                    $modeljc = $modeljc->where('pd.add_time','<=',$end_time);
                }
                $results = $modeljc->order('pd.add_time desc')->limit($page,$limit)->select();

                foreach ($results as $k => &$val) {
                    $val['deposit_time'] = unixtime_to_date($val['deposit_time']);
                    $arr_list[] = $val;
                }
                $total = $modeljc->count();
            }elseif ($type=='jc_out'){
                if ($t==2){
                    $lz_where[] = ['service_status','eq',3];
                    $lz_where[] = ['submit_status','eq',3];
                }else{
                    $lz_where[]  = array('service_status',"<",3);
                }

                $modeljc = new ServiceDepositModel();
                $modeljc = $modeljc->where($where)->where("pay_status = 1 and type= 1 ");

                if ($start_time){
                    $modeljc = $modeljc->where('add_time','>=',$start_time);
                }
                if ($end_time){
                    $modeljc = $modeljc->where('add_time','<=',$end_time);
                }


                $results = $modeljc->where($lz_where)->order('add_time desc')->limit($page,$limit)->select();
                foreach ($results as $k => &$val) {
                    $val['deposit_time'] = unixtime_to_date($val['deposit_time']);
                    $val['buyer_sex'] = $val['dealer_sex']==1?'男':($val['dealer_sex']==2?'女':'');
                    $arr_list[] = $val;
                }
                $total = $modeljc->where($where)->where($lz_where)->count();

            }elseif ($type=='qm'){
                $modelMove = Db::name("serviceMove");
                if ($t==2){
                    $lz_where[] = ['service_status','eq',3];
                    $lz_where[] = ['submit_status','eq',3];
                }else{
                    $lz_where[]  = array('service_status',"lt",3);
                    $lz_where[]  = array('submit_status',"gt",1);
                }
                $modelMove = $modelMove->alias('pd')
                    ->join("dj_customer_info c","pd.customer_id = c.id")
                    ->where($where)->where(['pd.pay_status'=>1])
                    ->where($lz_where);

                if ($start_time){
                    $modelMove = $modelMove->where('pd.add_time','>=',$start_time);
                }
                if ($end_time){
                    $modelMove = $modelMove->where('pd.add_time','<=',$end_time);
                }

                $results = $modelMove->order('pd.add_time desc')->limit($page,$limit)->select();
                foreach ($results as $k => &$val) {
                    $val['move_time'] = unixtime_to_date($val['move_time']);
                    $val['buyer_sex'] = $val['buyer_sex']==1?'男':($val['buyer_sex']==2?'女':'');
                    $arr_list[] = $val;
                }
                $total = $modelMove->count();
            }elseif ($type=='wb'){
                $modelWb = Db::name("serviceWeibao")->alias("pd")
                    ->join("dj_customer_info c","pd.customer_id = c.id")
                ->where(['pd.pay_status'=>1])->where($where);
                if ($t==2){
                    $lz_where[] = ['pd.service_status','>',3];
                }else{
                    $lz_where[]  = array('pd.service_status',"<",3);
                }
                if ($start_time){
                    $modelWb= $modelWb->where('pd.add_time','>=',$start_time);
                }
                if ($end_time){
                    $modelWb=  $modelWb->where('pd.add_time','<=',$end_time);
                }

                $modelWb = $modelWb->where($lz_where);
                $results = $modelWb->order('pd.add_time desc')->limit($page,$limit)->select();
                foreach ($results as $k => &$val) {
                    $val['register_time'] = unixtime_to_date($val['register_time']);
                    $val['deliver_time'] = unixtime_to_date($val['deliver_time']);
                    $val['buyer_sex'] = $val['buyer_sex']==1?'男':($val['buyer_sex']==2?'女':'');
                    $arr_list[] = $val;
                }
                $total = $modelWb->count();
            }elseif ($type=='wh'){
                $modelWh = Db::name("ProtectPayment");
                $modelWh = $modelWh->alias("pd")
                    ->join("dj_customer_info c","pd.customer_id = c.id")
                    ->where($where)->where(["pd.pay_status " => 1]);

                if ($t==2){
                    $modelWh = $modelWh->where('pd.end_time','>=',strtotime("+6 month"));
//                        ->where('pd.end_time','<=',strtotime("-1 month"));
                }else{
                    $modelWh = $modelWh->where('pd.end_time','>=',strtotime("-1 month"))
                        ->where('pd.end_time','<=',strtotime("+6 month"));
                }
                if ($start_time){
                    $modelWh = $modelWh->where('pd.add_time','>=',$start_time);
                }
                if ($end_time){
                    $modelWh = $modelWh->where('pd.add_time','<=',$end_time);
                }
                $results = $modelWh->order('pd.create_time desc')->limit($page,$limit)->select();

                foreach ($results as $k => &$val) {
                    $val['date_time'] =  unixtime_to_date(!empty($val['pay_time'])?$val['pay_time']:$val['create_time']);
                    $val['duration_name'] = $val['duration'].'年';
                    $val['sex_name'] = $val['sex']==1?'男':($val['sex']==2?'女':'');
                    $arr_list[] = $val;
                }
                $total = $modelWh->count();
            }elseif ($type=='dd'){
                $model_order = new OrdersModel();
                $model_order = $model_order->alias("o")->field("o.*,u.user_nickname")->where("o.tomb_id > 0 ");
                if ($start_time){
                    $model_order = $model_order->where('o.add_time','>=',$start_time);
                }
                if ($end_time){
                    $model_order = $model_order->where('o.add_time','<=',$end_time);
                }

                if ($t==2){
                    $model_order = $model_order->where("o.order_status = 3 ");
                }else if ($t==3){
                    $model_order = $model_order->where("o.order_status =2 ");
                }else{
                    $model_order = $model_order->where("o.order_status = 1 ");
                }
                $model_order = $model_order->where("o.customer_name","like","%".$buyer_name."%")->where("o.customer_phone","like","%".$buyer_phone."%");

                $user = Db::name("user")->where('id',cmf_get_current_admin_id())->find();
                // 判断如果墓位是艺术墓、且没有销售艺术墓权限时，不显示艺术墓订单
                if($user['if_sale'] == 0){
                    $model_order->join("dj_tombarea_row_col t","t.colID = o.tomb_id")->where('t.cate_id','<>',2);
                }
                $total = $model_order->count();
                $results = $model_order->join("dj_user u","u.id=o.edit_user","left")->order('add_time desc')->limit($page,$limit)->select();

                foreach ($results as $k => &$val) {
//                    $val['date_time'] = unixtime_to_date(!empty($val['pay_time']?$val['pay_time']:$val['create_time']));
                    $val['sell_time'] =  unixtime_to_date($val['sell_time']);
                    $val['date_time'] =  unixtime_to_date($val['add_time']);
                    $val['end_time'] =  unixtime_to_date($val['end_time']);
//                    $val['user_nickname'] =  cmf_get_admin_name($val['add_user']);
                    $val['rg_url'] =url('Orders/sale',array('orders_ids'=>$val['id'],'tomb_id'=>$val['tomb_id']));
                    $val['qx_dele_url'] =url('Orders/cancel',array('id'=>$val['id'],'tomb_num'=>$val['tomb_num']));
                    $val['djtx_wk_url'] = '';
                    if ($val['order_id']!=0){
                        $order_info = OrderModel::where(['orders_id'=>$val['id']])->find();
                        $val['djtx_wk_url'] =url('orders/signContract')."?order_id={$val['order_id']}&customer_id={$order_info['customer_id']}";
                    }

                    $val['hw_url'] = url('Orders/visua',array('orders_id'=>$val['id']));
                    $arr_list[] = $val;
                }
            }
            $result = array("total" => $total, "rows" => $arr_list,'arr'=>$type);
            return json($result);
        }

        $this->assign('t',$t);
        $this->assign('type',$type);
		return $this->fetch();
	}
	/*新增*/
	public function return1(){
	    if ($this->request->isPost()){
            $data = $this->request->param();

            if (empty($data['pay_voucher_img'])){
                return $this->error('请上传收款凭证！');
            }
            if (empty($data['refund_apply_time'])){
                return $this->error('请选择申请时间！');
            }
            if (empty($data['refund_account'])){
                return $this->error('请上传退款账户！');
            }

            $id = $data['id'];
            $Deposit_info = [
                'refund_pay'=>$data['refund_pay'],
                'refund_remark'=>$data['refund_remark'],
                'refund_apply_time'=>strtotime($data['refund_apply_time']),
                'refund_state'=>1,
                'pay_voucher_img'=>$data['pay_voucher_img'],
                'refund_amount'=>$data['refund_amount'],
                'refund_account'=>$data['refund_account'],
            ];

            PayDepositModel::update($Deposit_info,['id'=>$id]);

            $pay_info = OrderPayLogModel::where(['id'=>$data['pay_id']])->find();
            OrderPayLogModel::update(['refund_state'=>1],['id'=>$pay_info['id']]);
            $refund_info['type'] = 1;
            $refund_info['other_id'] =  $id;
            $refund_info['tomb_id'] = $data['tomb_id'];
            $refund_info['pay_id'] = $data['pay_id'];
            $refund_info['refund_sn'] =  PayRefundModel::getselfPayNum('add_time','refund_sn','TM');
            $refund_info['real_name'] =$pay_info['real_name'];
            $refund_info['real_phone'] =$pay_info['real_phone'];
            $refund_info['total_price'] =$pay_info['total_price'];
            $refund_info['refund_state'] = 1;
            $refund_info['refund_img'] = $data['pay_voucher_img'];
            $refund_info['refund_amount'] = $data['refund_amount'] ;
            $refund_info['refund_type'] = $data['refund_pay'];
            $refund_info['refund_apply_time'] = time();
            $refund_info['refund_apply_state'] = 1;
            $refund_info['add_user'] = cmf_get_current_admin_id();
            $refund_info['add_time'] = time();
            $refund_id = PayRefundModel::doDataAdd($refund_info);
            $result = PayDepositModel::update(['refund_id'=>$refund_id],['id'=>$id]);

            if ($result!=false){
                return $this->success('操作成功');
            }else{
                return $this->error('操作失败！');
            }
        }

	    $pdid = $this->request->param('pdid');
	    $dj_info = PayDepositModel::where(['id'=>$pdid])->find();
	    $order_info = OrderModel::where(['order_id'=>$dj_info['order_id']])->find();

        $dj_info['order_add_time'] = unixtime_to_date($order_info['add_time']);
        $dj_info['shoppingguide_name'] = cmf_get_admin_name($order_info['shoppingguide']);

		$this->assign("dj_info",$dj_info);
		$this->assign("page",'');
		return $this->fetch("return");
	}
	/*退订*/
	public function add(){
		$this->assign("page",'');
		return $this->fetch();
	}
}